<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.EquipmentDao"%>
<%@page import="com.shsh.safemanage.model.Equipment"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.util.Constants"%>
<%@page import="com.shsh.safemanage.model.PostCheck"%>
<%@page import="com.shsh.safemanage.model.PostcheckContent"%>
<%@page import="com.shsh.safemanage.model.PostcheckStandard"%>
<%@page import="com.shsh.safemanage.dao.PostCheckDao"%>
<%
Long postcheckStandardID = 0l;
String postcheckStandardIDStr = (String)request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_STANDARD_ID);
try{
	postcheckStandardID = Long.parseLong(postcheckStandardIDStr);
} catch (Exception e){
	
}

PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();
PostcheckStandard postcheckStandard = postcheckDao.getPostcheckStandardByID(postcheckStandardID);

PostcheckContent postcheckContent = postcheckDao.getPostcheckContentByID(postcheckStandard.getPostcheckContentID());
String initDateEndStr = Util.generateDateString(3, "yyyy-MM-dd");

User theLoginUser = (User) session.getAttribute(Constants.SESSION_USER);

session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
session.removeAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);
session.removeAttribute(Constants.SESSION_PROBLEM_EQUIPMENT);

WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();
UserAction userAction = ActionCreator.createUserAction();
Role role = userAction.getUserRole(request);
int intRole = writeProblemAction.calRole(null, null, null, role);
%>
<div class="hider" id="hider">
	<span>&nbsp;</span>
</div>
<div class="position">
	<img src="images/position2.jpg" style="padding: 7px 9px; float: left;" />当前位置：上海高桥分公司问题管理平台 &gt;&gt; <%=postcheckContent.getContent() %>岗检填报
</div>
<div class="tt"><%=postcheckContent.getContent() %>岗检填报卡</div>
<div class="content">
	<div style="float:right;">
				<input type="button" id="btnImport" name="btnImport" value="导入" style="margin-right:10px;margin-bottom:5px;" onclick="gotoImport()"/>
	</div>
	<table align="center" border="1" width="99%" cellpadding="0"
		cellspacing="0">
		<tr>
			<td class="td1" height="35" id="chooseDepartEquipTD">
				<%@include file="/write_problem_depart_equip_section.jsp" %>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">发现问题标题：（即：填写HSE观察作业卡上的“区域/设施”+“观察的作业”）</span>
				<br />
				<input type="text" style="margin-left: 60px" id="problemTitle" size="45" />
				<input type="checkbox" id="isRegular" name="isRegular" style="margin-left: 10px" onclick="setIsRegular()" />
				<label for="isRegular" style="margin-left: 3px">检查正常</label>
				<input type="checkbox" id="isSanwei" name="isSanwei" style="margin-left: 10px" onclick="setIsSanwei()" />
				<label for="isSanwei" style="margin-left: 3px">“三违”现象</label>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">发现问题内容：（即：HSE观察作业卡上的“观察事实描述”）</span>
				<br />
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="problemContent"></textarea>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">整改措施：（即：HSE观察作业卡上的“立即采取的措施”）</span>
				<br />
				<textarea style="width: 600px; height: 100px; margin-left: 60px" id="zhenggai_cuoshi"></textarea>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<div style="margin-left:40px;margin-right:40px;">工作标准与要求：<%=postcheckStandard.getStandard()%></div>			
				<span style="margin-left: 40px" class="span1">扣分值：</span><input type="text" id="grade" name="grade" value="0"/> 
				<span style="margin-left: 20px" class="span1">本条工作标准中的扣分标准：</span><input type="text" id="gradeStandard" name="gradeStandard" value="<%=postcheckStandard.getGradeStandard()%>" readonly="readonly"/>
				<br />
				<span style="margin-left: 40px" class="span1">要求整改完成日期：</span><input type="text" id="completeDate" size="15" 
					onfocus="WdatePicker({maxDate:'#F{\'2050-12-31\'}'})" value="<%=initDateEndStr%>"/>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<%
				if ((intRole & (Constants.ROLE_TYPE_ZHUANGZHIZHANG)) == 0) {
				%>
				<div id="informPersonDiv">
				<%@include file="/write_problem_informed_person.jsp" %>
				</div>
				<%
				}
				%>
				<div id="chooseManageGroupUserDiv">
				<%@include file="/managegroup_select/manage_check_samepage_section.jsp" %>
				</div>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<span style="margin-left: 40px" class="span1">填报人及时间：</span>
				<span><%=theLoginUser.getDepartment1Name()%>-<%=theLoginUser.getDepartment2Name()%>-<%=theLoginUser.getDepartment3Name()%></span>
				<span><%=theLoginUser.getName()%></span>
				<span><%=Util.formatDateTime(Calendar.getInstance())%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" height="35">
				<input type="button" style="margin-left: 300px" value="提 交" onclick="writePostcheck()" />
				<input type="button" style="margin-left: 30px" value="取 消" onclick="cancelPostcheck()" />
			</td>
		</tr>
	</table>
</div>

<script type="text/javascript">
function gotoImport(){
	loadPageSection(
			'<%=request.getContextPath()%>/postcheck/problems_tobe_converted.jsp', 
			'mainContentDiv', 
			{
				postcheckID: <%=postcheckStandard.getPostcheckID()%>
			}
	);
}

function writePostcheck() {
	if (getElementValueByID('problemTitle') == '') {
		$.dialog.alert('请输入标题');
		return;
	}
	
	var problemContent = getElementValueByID('problemContent');
	if (problemContent == null || problemContent.length < 5) {
		$.dialog.alert('在发现问题内容一栏里必需填报岗检情况描述');
		return;
	}
	if (getElementValueByID('zhenggai_cuoshi') == '') {
		$.dialog.alert('请输入整改措施');
		return;
	}

	if (document.getElementById('equipID') != null) {
		if (getElementValueByID('equipID') == '') {
			$.dialog.alert('请选择发生问题的装置');
			return;
		}
	}
	
	var grade = getElementValueByID('grade');
	if(grade == null ||  grade == '' || grade == 'undefined') {
		$.dialog.alert('请在扣分值输入一个数字');
		return;
	}
	
	var strExp = <%=Constants.REGX_GRADE%>;
	if (!strExp.test(grade)){
		$.dialog.alert('扣分值必须为非负数');
		return;
	}	
	
	var userIDs = '';
	var manageGroupUserCheckEles = document.getElementsByName('manageGroupUserCheck');
	for (var i = 0; i != manageGroupUserCheckEles.length; i++) {
		if (manageGroupUserCheckEles[i].checked) {
			if (userIDs == '') {
				userIDs = userIDs + manageGroupUserCheckEles[i].value;
			} else {
				userIDs = userIDs + ',' + manageGroupUserCheckEles[i].value;
			}
		}
	}

	var isRegular = 0;
	if (document.getElementById('isRegular').checked) {
		isRegular = 1;
	}
	
	if ($.dialog.confirm('您确定提交此问题吗？', function(){
		openWaitingDialog('waitingDialog');
		$.ajax({
			type:'post',
			url:'<%=request.getContextPath()%>/postcheck/write_postcheck_process.jsp',
			data:{
				isRegular: isRegular,
				isSanwei: $('#isSanwei').attr('checked'),
				problemTitle: getElementValueByID('problemTitle'),
				problemContent: getElementValueByID('problemContent'),
				zhenggai_cuoshi: getElementValueByID('zhenggai_cuoshi'),
				equipID: getElementValueByID('equipID'),
				userID: userIDs,
				postcheckID: <%=postcheckStandard.getPostcheckID()%>,
				postcheckPatternID: <%=postcheckStandard.getPostcheckPatternID()%>,
				postcheckContentID: <%=postcheckStandard.getPostcheckContentID()%>,
				postcheckStandardID: <%=postcheckStandard.getId()%>,
				grade: grade,
				completeDate: getElementValueByID('completeDate'),
				depart1ID: getElementValueByID('depart1')
			},
			success:function(data,state,xmlhttp){
				var obj = null;
				try {
					obj = eval('(' + data + ')');
				} catch (e) {
				}
				if (typeof(obj.isSuccess) != 'undefined') {
					closeWaitingDialog('waitingDialog');
					if (obj.isSuccess == 'true') {
						$.dialog('问题填报成功！', function() { 
							loadPageSection('<%=request.getContextPath()%>/postcheck/postcheck_standard_list.jsp', 'mainContentDiv', {postcheckID: <%=postcheckStandard.getPostcheckID()%>}, true);
						});
					} else {
						$.dialog.alert(obj.error);
					}
				}
			}
		});
	}, function(){}));
}

function cancelPostcheck() {
	if ($.dialog.confirm('您确定取消此问题吗？', function(){
		loadPageSection('<%=request.getContextPath()%>/postcheck/postcheck_standard_list.jsp', 'mainContentDiv', {postcheckID: <%=postcheckStandard.getPostcheckID()%>}, true);
	}, function(){}));
}

function changeEquip() {
	var equipID = getElementValueByID('equipID');
	loadPageSection('<%=request.getContextPath()%>/write_problem_informed_person.jsp', 'informPersonDiv', {equipID: equipID}, false);
}

function setIsRegular(){
	if($('#isSanwei').attr('checked') == true){
		$.dialog.alert('已选择“三违”现象');
		$('#isRegular').attr('checked', false);
		return;
	}
	
	var isRegularElement = document.getElementById('isRegular');
	if (isRegularElement.checked) {
		var content = getElementValueByID('problemContent');
		if(content == null || content == '') {
			setElementValueByID('problemContent', content + '经检查正常');
		} else {
			setElementValueByID('problemContent', content + '\r经检查正常');
		}
		var zhenggai = getElementValueByID('zhenggai_cuoshi');	
		if(zhenggai == null || zhenggai == '') {
			setElementValueByID('zhenggai_cuoshi', zhenggai + '检查正常');
		} else {
			setElementValueByID('zhenggai_cuoshi', zhenggai + '\r检查正常');
		}
		
		$("#grade").attr("readonly", "readonly");
	} else {
		setElementValueByID('problemContent', '');
		setElementValueByID('zhenggai_cuoshi', '');
		$("#grade").attr("readonly", "");
	}
}

function setIsSanwei(){
	if($('#isRegular').attr('checked') == true){
		$.dialog.alert('已选择检查正常');
		$('#isSanwei').attr('checked', false);
		return;
	}
}

<%
if ((intRole & Constants.ROLE_TYPE_ZUYUAN | Constants.ROLE_TYPE_KEYUAN
		| Constants.ROLE_TYPE_FUKEZHANG | Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUCHUZHANG
		| Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_ZONGSHI | Constants.ROLE_TYPE_FUZONGJINGLI
		| Constants.ROLE_TYPE_ZONGJINGLI) > 0) {
%>
changeEquip();
<%
}
%>
</script>